package com.jushu.storbox;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.text.InputType;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jushu.storbox.actions.Actions;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.constant.ExceptionConstant;
import com.jushu.storbox.database.DAOFactory;
import com.jushu.storbox.database.IUserDao;
import com.jushu.storbox.entities.User;
import com.jushu.storbox.filesystem.iFileManager;
import com.jushu.storbox.handler.ClientExceptionRelateHandler;
import com.jushu.storbox.service.CloudDriveService;
import com.jushu.storbox.service.ICloudDriveService;
import com.jushu.storbox.util.AESEncryptorUtil;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.view.dialog.ClouddriveDialog;
import com.jushu.storbox.view.dialog.MyPopupWindow;
import com.jushu.storbox.view.dialog.TextProgressDialog;
import com.jushu.storbox.view.viewImpl.SlipButton.OnChangedListener;

/**
 *
 * LoginActivity
 *
 */
public class LoginActivity extends BaseActivity {

	/** loginIn bt */
	private View loginBt;

	/** setting bt */
	private ImageView login_setting_im;

	/** setting popupwindow view */
	private View window_loginsetting;

	/** login base layout */
	private View login_rl;

	/** setting popupwindow Cancel bt */
	private Button settingCancel;

	/** setting popupwindow Define bt */
	private Button settingDefine;

	/** popupwindow instance */
	private MyPopupWindow pwPopupWindow;

	/** setting popupwindow instance serviceadd et */
	private EditText login_setting_serviceadd_et;

	/** login userName et */
	private EditText userName_et;

	/** login userPassword et */
	private EditText userPassword_et;

	/** login confirm et */
	private Button confirm_btnButton;

	/** popupwindow instance */
	private MyPopupWindow user_window = null;// MyPopupwindow

	/** save data in sharedpreferences */
	// private SharedPreferences settings;

	/** sharedpreferences editor set data */
	// private Editor editor;

	/** popup window view */
	private View window_view;

	/** login userpass cb */
	private CheckBox login_userpass_cb;
	private ImageButton userpass_on_and_off;

	/** login Progress Dialog */
	private ProgressDialog progressDialog;

	private String TAG = "LoginActivity";

	private Actions actions;
	private boolean flag_hide_show_password = true;
	private ImageView hide_show_password;
	/** ShareDriveService */
	private ICloudDriveService mIcloudDriveService;

	private int mWindowWidth;
	private int mWindowHeight;
	private ImageView mIvWelcome;
	private View mLayoutInput;
	private View mTvDesign;
	private ImageView oneboxTxt;

	//
	private InputMethodManager inputManager;

	/** ON SERVICE CONNECTED */
	private ServiceConnection mServiceConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// get cloud drive service instance
			mIcloudDriveService = ((CloudDriveService.CenterShareDriveServiceServiceBinder) service).getService();
			actions = new Actions(LoginActivity.this);
			LogUtil.i(TAG, "onServiceConnected...");
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			LogUtil.e(TAG, "onServiceDisconnected...");
		}
	};
	private OnClickListener hideShowListener = new OnClickListener() {//密码锁点击，切换成显示或隐藏密码形式
		@Override
		public void onClick(View v) {
			//假如隐藏，则点击显示密码
			if(flag_hide_show_password){
				userPassword_et.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				flag_hide_show_password = false;
			}
			//假如显示密码，则点击隐藏
			else{
				userPassword_et.setInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_PASSWORD);
				flag_hide_show_password = true;
			}

		}
	};


	/**
	 * onCreate in BaseActivity
	 *
	 * @param savedInstanceState
	 *            Bundle
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LogUtil.i(TAG, "onCreate()");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_logine);
		LogUtil.i(TAG, "onCreate()");
		// ActivityTaskManager.getInstance().putActivity(TAG,
		// LoginActivity.this);
		initWindowParam();
		initView();
		// init find view by id
		initFindViewById();
		// init data
		initData();
		// init listenser
		initListenser();
		// bindservice
		bindICloudDriveService();
		userName_et.setOnFocusChangeListener(changeListenerUsername);
		userPassword_et.setOnFocusChangeListener(changeListenerPassword);
	}

	private void bindICloudDriveService() {
		Intent intent = new Intent(this, CloudDriveService.class);
		if (null != mServiceConnection) {
			bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
		}
	}

	/**
	 * LoginActivity init onclick listenser
	 */
	private void initListenser() {
		loginBt.setOnClickListener(loginListenser);
		login_setting_im.setOnClickListener(settingListenser);
		// settingCancel.setOnClickListener(settingCancelListenser);
		login_userpass_cb.setOnCheckedChangeListener(checkListenser);
		userpass_on_and_off.setOnTouchListener(new PassOnAndOffListener());
		// settingDefine.setOnClickListener(settingDefineLinstenser);
		hide_show_password.setOnClickListener(hideShowListener);
	}

	private class PassOnAndOffListener implements OnTouchListener {
		@Override
		public boolean onTouch(View arg0, MotionEvent arg1) {
			switch (arg1.getAction()) {
			case MotionEvent.ACTION_DOWN:
				userPassword_et.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				break;
			case MotionEvent.ACTION_UP:
				userPassword_et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			default:
				break;
			}
			return false;
		}
	}

	/**
	 * LoginActivity init data
	 */
	private void initData() {

		if (null == inputManager) {
			inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		}

		loginBt.setClickable(true);
		SharedPreferences settings = getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
		Editor editor = settings.edit();
		// login_setting_serviceadd_et.setText(settings.getString(
		// ClientConfig.SERVER_ADDRESS, ""));
		if ("".equals(settings.getString(ClientConfig.SERVER_ADDRESS, "").toString().trim())) {
			editor.putString(ClientConfig.SERVER_ADDRESS, ClientConfig.SERVICEADD);
			// login_setting_serviceadd_et.setText(ClientConfig.SERVICEADD);
			editor.commit();
		}
		if (settings.getBoolean(ClientConfig.AUTOLOGIN_SETTING, true)) {
			userName_et.setText(PublicTools.getLoginUserName(LoginActivity.this));
			userPassword_et.setText(PublicTools.getLoginUserPassFail(LoginActivity.this));

		} else {
			userName_et.setText(PublicTools.getLoginUserName(LoginActivity.this));
		}
		login_userpass_cb.setChecked(settings.getBoolean(ClientConfig.AUTOLOGIN_SETTING, true));

		if (null == progressDialog) {
			progressDialog = new TextProgressDialog(LoginActivity.this, R.style.dialog);
			// progressDialog = new TextProgressDialog(LoginActivity.this);

			Window wd = progressDialog.getWindow();
			WindowManager.LayoutParams lp = wd.getAttributes();
			lp.alpha = 0.5f;
			wd.setAttributes(lp);

		}
		if (!settings.getBoolean(ClientConfig.AUTOLOGIN, false)) {
			PublicTools.setLoginUserPassFail(LoginActivity.this, "");
		}
	}

	/**
	 * LoginActivity init find view by id
	 */
	public void initFindViewById() {
		loginBt = findViewById(R.id.activity_login_bt);
		login_setting_im = (ImageView) findViewById(R.id.login_setting_im);

		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		login_rl = findViewById(R.id.login_rl);

		// zwx163376
		userName_et = (EditText) findViewById(R.id.lgoin_username_et);
		userPassword_et = (EditText) findViewById(R.id.lgoin_password_et);
		login_userpass_cb = (CheckBox) findViewById(R.id.lgoin_userpass_cb);
		userpass_on_and_off = (ImageButton) findViewById(R.id.lgoin_userpass_on_and_off);
		hide_show_password = (ImageView) findViewById(R.id.hide_show_password_iv);
	}
	private OnFocusChangeListener changeListenerUsername=new OnFocusChangeListener() {

		@Override
		public void onFocusChange(View view, boolean hasFocus) {
			EditText v=(EditText)view;
			if (hasFocus) {
				String hint=v.getHint().toString();
				v.setTag(hint);
				v.setHint("");
			}
			else {

				v.setHint(v.getTag().toString());
			}
		}
	};
private OnFocusChangeListener changeListenerPassword=new OnFocusChangeListener() {

		@Override
		public void onFocusChange(View view, boolean hasFocus) {
			EditText v=(EditText)view;
			if (hasFocus) {
				String hint=v.getHint().toString();
				v.setTag(hint);
				v.setHint("");
			}
			else {

				v.setHint(v.getTag().toString());
			}
		}
	};
	/**
	 * LoginActivity checkbox Listener
	 */
	private OnCheckedChangeListener checkListenser = new OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
			if (arg0.equals(login_userpass_cb)) {
				SharedPreferences settings = getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
				Editor editor = settings.edit();
				editor.putBoolean(ClientConfig.AUTOLOGIN_SETTING, arg1);
				if (!arg1) {
					editor.putBoolean(ClientConfig.AUTOLOGIN, arg1);
				} else {
					if (0 != PublicTools.getClientUserName(LoginActivity.this).length() && 0 != PublicTools.getClientUserPassFail(LoginActivity.this).length()) {
						editor.putBoolean(ClientConfig.AUTOLOGIN, arg1);
						editor.putBoolean(ClientConfig.AUTOLOGIN_SETTING, arg1);
					}
				}
				editor.commit();
			}
		}
	};

	/**
	 * LoginActivity login in Listener
	 */
	private OnClickListener loginListenser = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			if (null != mIcloudDriveService) {
				if (PublicTools.mdmRootCheck()) {
					userPassword_et.setText("");
					Dialog dialog = mIcloudDriveService.buildNowIsRootDialog();

					dialog.setCanceledOnTouchOutside(false);
					dialog.setCancelable(false);
					dialog.show();
					return;
				}
			}
			SharedPreferences settings = getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
			Editor editor = settings.edit();
			if (settings.getBoolean(ClientConfig.AUTOLOGIN, true)) {
				PublicTools.setLoginUserName(LoginActivity.this, userName_et.getText().toString().trim());
				PublicTools.setLoginUserPassFail(LoginActivity.this, userPassword_et.getText().toString().trim());
			}

			if (editTextIsNull(userName_et)) {
				showDialog(LoginActivity.this, LoginActivity.this.getString(R.string.usernameisnull), R.layout.window_dialog_new, login_rl);
			} else if (editTextIsNull(userPassword_et)) {
				showDialog(LoginActivity.this, LoginActivity.this.getString(R.string.userpasswordisnull), R.layout.window_dialog_new, login_rl);
			} else {
				if (null != editor) {
					saveLoginInfo(editor);
				}

				if (ShareDriveApplication.getInstance().getWifiController().getNetworkState()) {
					if (progressDialog != null) {
						progressDialog.show();
					}
					new Thread(new Runnable() {

						@Override
						public void run() {
							actions.loginIn(mIcloudDriveService, userName_et.getText().toString(), userPassword_et.getText().toString(), exceptionHandlers,
									exceptionHandlers);
						}
					}).start();
				} else {
					String userName = userName_et.getText().toString();
					String password = userPassword_et.getText().toString();
					User localUser = null;
					try {
						String encryPass = AESEncryptorUtil.encryptAndBase64(AESEncryptorUtil.getSpesPwd(), password);
						IUserDao userDao = DAOFactory.instance(LoginActivity.this).getUserDao();
						localUser = userDao.getUser(userName, encryPass);
					} catch (Exception e) {
						LogUtil.e(TAG, e.getMessage());
					}

					if (localUser != null) {
						ShareDriveApplication.getInstance().setCurrentUser(localUser);
						ShareDriveApplication.getInstance().setWnerID(localUser.getUserId());
						PublicTools.setClientUserPassFail(LoginActivity.this, password);
						PublicTools.setClientUserName(LoginActivity.this, userName);

						Intent intent = new Intent();
						intent.setClass(LoginActivity.this, LoginWaitActivity.class);
						startActivity(intent);
						LoginActivity.this.finish();
						loginBt.setClickable(false);
						SharedPreferences setting = getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
						Editor editors = settings.edit();
						/** set auto login state */
						if (setting.getBoolean(ClientConfig.AUTOLOGIN_SETTING, true)) {
							editors.putBoolean(ClientConfig.AUTOLOGIN, true);
						}
						/** set loginwait state to start activity 8 */
						editors.putBoolean(ClientConfig.LOGINWAITACTIVITY, false);
						editors.commit();
					} else {
						Toast.makeText(LoginActivity.this, getString(R.string.allfile_notwork_obstructed), Toast.LENGTH_SHORT).show();
					}
				}
			}
		}
	};

	/**
	 * LoginActivity setting onclick Listener
	 */
	private OnClickListener settingListenser = new OnClickListener() {

		@Override
		public void onClick(View arg0) {

			final ClouddriveDialog dialog = new ClouddriveDialog(LoginActivity.this, R.style.dialog_upload, R.layout.dialog_setting_server_site);
			dialog.setCanceledOnTouchOutside(true);
			View parentView = dialog.getConventView();
			WindowManager windowManager=getWindowManager();
			Display display=windowManager.getDefaultDisplay();
			dialog.setWidth(display.getWidth());
			dialog.setPosition(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL);
			View btnSure = parentView.findViewById(R.id.dialog_add_file_ok_bt);
			View btnCancel = parentView.findViewById(R.id.dialog_add_file_cancal_bt);
			final EditText etServerSite = (EditText) parentView.findViewById(R.id.login_setting_serviceadd_et);
			View btnRecovery = parentView.findViewById(R.id.tv_recovery);

			SharedPreferences settings = getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
			String addressInPreference = settings.getString(ClientConfig.SERVER_ADDRESS, "");

			if (addressInPreference.trim().equals("")) {
				etServerSite.setText(ClientConfig.SERVICEADD);
			} else {
				etServerSite.setText(addressInPreference);
			}

			btnSure.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog.dismiss();
					String nowAddress = etServerSite.getText().toString().trim();
					SharedPreferences settings = getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
					Editor editor = settings.edit();
					editor.putString(ClientConfig.SERVER_ADDRESS, nowAddress);
					editor.commit();
				}
			});

			btnCancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dialog.dismiss();

				}
			});

			btnRecovery.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					SharedPreferences settings = getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
					Editor editor = settings.edit();
					editor.putString(ClientConfig.SERVER_ADDRESS, ClientConfig.SERVICEADD);
					editor.commit();

					etServerSite.setText(ClientConfig.SERVICEADD);
				}
			});

			dialog.show();
		}
	};

	public void showDialog(Context context, String str, int Resouce, View parent) {
		final ClouddriveDialog dialog = new ClouddriveDialog(context, R.style.dialog_upload, Resouce);
		dialog.setCanceledOnTouchOutside(true);
		WindowManager windowManager=getWindowManager();
		Display display=windowManager.getDefaultDisplay();
		dialog.setWidth(display.getWidth());
		dialog.setPosition(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL);
		View contextView = dialog.getConventView();
		TextView info = (TextView) contextView.findViewById(R.id.infomations);
		View confirm_btnButton = (Button) contextView.findViewById(R.id.confirm_btn);
		confirm_btnButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		info.setText(str);
		dialog.show();
	}

	/**
	 * save login user information
	 *
	 * @param editor
	 *            Editor
	 */
	public void saveLoginInfo(Editor editor) {
		PublicTools.setLoginUserName(LoginActivity.this, userName_et.getText().toString().trim());
		PublicTools.setLoginUserPassFail(LoginActivity.this, userPassword_et.getText().toString().trim());
		editor.putInt("LOGINSTATE",  1);
		editor.commit();
	}

	/**
	 * LoginActivity onDestroy
	 */
	@Override
	protected void onDestroy() {
		LogUtil.i(TAG, "onDestroy()");
		SharedPreferences settings = getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
		Editor editor = settings.edit();
		editor.commit();
		// ActivityTaskManager.getInstance().removeActivity(TAG);
		if (mServiceConnection != null) {
			unbindService(mServiceConnection);
		}
		super.onDestroy();
	}

	/**
	 * LoginActivity onPause
	 */
	@Override
	protected void onPause() {
		LogUtil.i(TAG, "onPause()");
		SharedPreferences settings = getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
		Editor editor = settings.edit();
		editor.putLong("RECORDTIME", System.currentTimeMillis());
		editor.commit();
		super.onPause();
	}

	/**
	 * editText control Is Null?
	 *
	 * @param editText
	 *            EditText control
	 * @return boolean true:null,false:not null
	 */
	private boolean editTextIsNull(EditText editText) {
		boolean flag = true;
		String text = editText.getText().toString();
		if ("".equals(text) || 0 == text.length()) {
			flag = true;
		} else {
			flag = false;
		}
		return flag;
	}

	ClientExceptionRelateHandler exceptionHandlers = new ClientExceptionRelateHandler() {

		@Override
		protected void onHandlerBefore(Message msg, String errorCode) {
			if (progressDialog != null && progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			super.onHandlerBefore(msg, errorCode);
		}

		@Override
		protected void onException_401_ClientUnauthorized(Message msg, String errorCode) {
			super.onException_401_ClientUnauthorized(msg, errorCode);
			if (ExceptionConstant.clientunauthorized.equalsIgnoreCase((String) msg.obj)) {
				if (null != mIcloudDriveService) {
					mIcloudDriveService.clearCloudDriveCache(null);
					mIcloudDriveService.creatMobileForbiddenDialog();
				}
			}
		}

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ClientConfig.SENDFROMLOGIN:
				SharedPreferences settings = getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
				Editor editor = settings.edit();
				/** set auto login state */
				if (settings.getBoolean(ClientConfig.AUTOLOGIN_SETTING, true)) {
					editor.putBoolean(ClientConfig.AUTOLOGIN, true);
				}

				/** set loginwait state to start activity 8 */
				editor.putBoolean(ClientConfig.LOGINWAITACTIVITY, false);
				editor.commit();
//				PublicTools.setClientUserName(LoginActivity.this, PublicTools.getLoginUserName(LoginActivity.this));
//				PublicTools.setClientUserPassFail(LoginActivity.this, PublicTools.getLoginUserPassFail(LoginActivity.this));

				Intent intent = new Intent();
				intent.setClass(LoginActivity.this, LoginWaitActivity.class);
				startActivity(intent);
				LoginActivity.this.finish();
				loginBt.setClickable(false);

				if (progressDialog != null) {
					progressDialog.dismiss();
					progressDialog = null;
				}

				Message upgrage = exceptionHandlers.obtainMessage();
				upgrage.what = ClientConfig.SENDFROMUPGRAGE;
				upgrage.sendToTarget();
				break;

			case ClientConfig.SEND_FROM_GET_UAM_ADDRESS:
				break;

			case ClientConfig.SEND_FROM_GET_UFM_ADDRESS:
				break;

			default:
				super.handleMessage(msg);
			}

		}

		@Override
		public Context getContext() {
			return LoginActivity.this;
		};
	};

	@SuppressWarnings("deprecation")
	private void initWindowParam() {
		Display display = getWindowManager().getDefaultDisplay();
		mWindowWidth = display.getWidth();
		mWindowHeight = display.getHeight();
	}

	private void initView() {

		ImageView oneboxLabel = (ImageView) findViewById(R.id.iv_onebox_label);
		if (PublicTools.isLocaleLanguageCN()) {
			oneboxLabel.setImageResource(R.mipmap.login_logo);

		} else {
			oneboxLabel.setImageResource(R.mipmap.login_logo_en);

		}
		oneboxTxt = (ImageView) findViewById(R.id.iv_onebox_txt);

		mIvWelcome = (ImageView) findViewById(R.id.iv_welcome);
		mIvWelcome.setVisibility(View.INVISIBLE);
		mLayoutInput = findViewById(R.id.layout_username_and_pass);
		mTvDesign = findViewById(R.id.tv_design);

		// Animation
//		AnimationSet setAnim = new AnimationSet(true);
//		setAnim.setDuration(1000);
//		setAnim.setFillAfter(true);
//		setAnim.setRepeatCount(1);
//		ScaleAnimation scaleAnim = new ScaleAnimation(1f, 0.85f, 1f, 0.85f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//		TranslateAnimation translateAnim = new TranslateAnimation(0, 0, 0, -mWindowHeight / 4);
//
//
//
//		setAnim.addAnimation(scaleAnim);
//		setAnim.addAnimation(translateAnim);
//		setAnim.setStartOffset(500);
//		oneboxLabel.startAnimation(setAnim);



		ObjectAnimator animator1 = ObjectAnimator.ofFloat(oneboxLabel, "translationY", 0.5f, -mWindowHeight / 4);
		ObjectAnimator anim = ObjectAnimator.ofFloat(oneboxLabel, "scaleX",  1f,  1.05f, 0.85f);
		ObjectAnimator anim1 = ObjectAnimator.ofFloat(oneboxLabel, "scaleY", 1f,  1.05f, 0.85f);
		AnimatorSet set = new AnimatorSet();

		(set.play(animator1)).with(anim).with(anim1);
		set.setDuration(1500);
		set.start();

		AnimationSet setAnim1 = new AnimationSet(true);
		setAnim1.setDuration(1000);
		setAnim1.setFillAfter(true);
		setAnim1.setRepeatCount(1);
//		translateAnim = new TranslateAnimation(0, 0, 0, -mWindowHeight / 5);
//		setAnim1.addAnimation(scaleAnim);
//		setAnim1.addAnimation(translateAnim);
		// start delay
		setAnim1.setStartOffset(500);
		oneboxTxt.startAnimation(setAnim1);

		mIvWelcome.setTranslationY(-mWindowHeight / 5 - 20);

		AlphaAnimation alphaAnim = new AlphaAnimation(0, 1);
		alphaAnim.setFillAfter(true);
		alphaAnim.setDuration(1500);
		alphaAnim.setStartOffset(800);
		mLayoutInput.setAnimation(alphaAnim);

		setAnim1.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// mIvWelcome.scrollBy(0, -mWindowHeight/5);
				// oneboxTxt.setImageResource(R.drawable.onebox_txt_after_anim);
				// oneboxTxt.measure(0, 0);
				RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) mIvWelcome.getLayoutParams();
				params.addRule(RelativeLayout.ALIGN_LEFT, R.id.iv_onebox_txt);
				params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.iv_onebox_txt);
				mIvWelcome.setLayoutParams(params);
				// mIvWelcome.setVisibility(View.VISIBLE);
				// oneboxTxt.setImageResource(R.drawable.onebox_txt_after_anim);
				mTvDesign.setVisibility(View.GONE);
				// mLayoutInput.setVisibility(View.VISIBLE);

				AlphaAnimation alphaAnim = new AlphaAnimation(0, 1);
				alphaAnim.setFillAfter(true);
				alphaAnim.setDuration(300);
				mIvWelcome.setAnimation(alphaAnim);
				// mLayoutInput.setAnimation(alphaAnim);
			}
		});

	}

	@Override
	protected void onResume() {
		if (PublicTools.isLocaleLanguageCN()) {
			mIvWelcome.setImageResource(R.mipmap.welcome);
		} else {
			mIvWelcome.setImageResource(R.mipmap.welcome_b);
		}
		login_userpass_cb.setText(R.string.login_auto);
		super.onResume();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		PublicTools.hideSoftInput(inputManager, userName_et);
		return super.onTouchEvent(event);
	}
}
